package com.kashi.controller;

import com.kashi.pojo.Goods;
import com.kashi.pojo.Result;
import com.kashi.service.GoodsService;
import com.kashi.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api")
public class SearchController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private SearchService searchService;

    @GetMapping("/search")
    public Result searchGoods(@RequestParam String keyword,
                              @RequestParam(required = false) String category){
        log.info("查询商品，参数：{}，{}",keyword,category);
        List<Goods> goodsList = goodsService.list(keyword, category);
        return Result.success(goodsList);
    }

    @PostMapping("/history")
    public Result saveHistory(@RequestParam Integer id, @RequestParam String keyword){
        log.info("存储搜索历史：{},{}",id,keyword);
        //调用service存储
        searchService.saveHistory(id,keyword);
        return Result.success();
    }

    @GetMapping("/history")
    public Result readHistory(Integer id){
        log.info("读取用户{}搜索历史",id);
        //调用service读取
        List<String> historyList = searchService.readHistory(id);
        return Result.success(historyList);
    }

}
